package dice.sinanya.start;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;

public class StartCoolQ implements FindDir {
    private static final Logger logger = LogManager.getLogger(StartCoolQ.class.getName());

    public StartCoolQ() {

    }

    public void start(String fileName) {
        try {
            File exe = findDir(fileName);
            Runtime mt = Runtime.getRuntime();
            mt.exec(exe.getAbsolutePath());
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
    }

    @Override
    public File findDir(String fileName) throws IOException {
        File myDir = findMyDir();
        File coolQDir = new File(URLDecoder.decode(myDir.getPath(), "UTF-8"), "CoolQ");
        if (coolQDir.exists() && coolQDir.isDirectory()) {
            File result = new File(URLDecoder.decode(coolQDir.getPath(), "UTF-8"), fileName);
            if (result.exists() && result.isFile()) {
                return result;
            }
        }
        throw new IOException("您所查找的文件:\t" + fileName + "在:\t" + coolQDir.getPath() + "没有找到");
    }
}
